#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\models\marketmodels\models\piecewiseconstantvariance.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

#undef HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Models { namespace Marketmodels { namespace Models {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IPiecewiseConstantVariance
	public ref class CPiecewiseConstantVariance  : 
            public Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance
	{
	protected: 
		boost::shared_ptr<QuantLib::PiecewiseConstantVariance>* _ppPiecewiseConstantVariance;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::PiecewiseConstantVariance>* _phPiecewiseConstantVariance;
#endif
		Object^ _PiecewiseConstantVarianceOwner;     // reference to object that manages the storage for this object
	internal:
        CPiecewiseConstantVariance (boost::shared_ptr<QuantLib::PiecewiseConstantVariance>& childNative, Object^ owner);
        CPiecewiseConstantVariance (QuantLib::PiecewiseConstantVariance& childNative, Object^ owner);
        CPiecewiseConstantVariance (CPiecewiseConstantVariance^ copy);
        CPiecewiseConstantVariance (PLATFORM::Type^ t);
#ifdef STRUCT
        CPiecewiseConstantVariance (QuantLib::PiecewiseConstantVariance childNative);
#endif       
#ifdef HANDLE
		CPiecewiseConstantVariance (QuantLib::Handle<QuantLib::PiecewiseConstantVariance>& childNative, Object^ owner);
		CPiecewiseConstantVariance (QuantLib::Handle<QuantLib::PiecewiseConstantVariance> childNative);
#endif
		virtual ~CPiecewiseConstantVariance ();
		!CPiecewiseConstantVariance ();

	internal:
		QuantLib::PiecewiseConstantVariance& GetReference ();
		boost::shared_ptr<QuantLib::PiecewiseConstantVariance>& GetShared ();
		QuantLib::PiecewiseConstantVariance* GetPointer ();
        void SetPiecewiseConstantVariance (boost::shared_ptr<QuantLib::PiecewiseConstantVariance> native)
        {
            if (_ppPiecewiseConstantVariance != NULL)
                delete _ppPiecewiseConstantVariance;
            _ppPiecewiseConstantVariance = new boost::shared_ptr<QuantLib::PiecewiseConstantVariance> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::PiecewiseConstantVariance>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property Cephei::Core::IVector<Double>^ RateTimes 
        {
		    virtual Cephei::Core::IVector<Double>^ get () ;
        }
		virtual Double TotalVariance (UInt64 i) ;
		virtual Double TotalVolatility (UInt64 i) ;
		virtual Double Variance (UInt64 i) ;
        property Cephei::Core::IVector<Double>^ Variances 
        {
		    virtual Cephei::Core::IVector<Double>^ get () ;
        }
        property Cephei::Core::IVector<Double>^ Volatilities 
        {
		    virtual Cephei::Core::IVector<Double>^ get () ;
        }
		virtual Double Volatility (UInt64 i) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IPiecewiseConstantVariance Vector interface
	ref class CPiecewiseConstantVarianceVector sealed : 
							    public Core::INativeVector<IPiecewiseConstantVariance^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IPiecewiseConstantVariance^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::PiecewiseConstantVariance>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::PiecewiseConstantVariance>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CPiecewiseConstantVarianceVector ();
#ifdef STRUCT
		CPiecewiseConstantVarianceVector (std::vector<QuantLib::PiecewiseConstantVariance>& vec, Object^ owner);
		CPiecewiseConstantVarianceVector (std::vector<QuantLib::PiecewiseConstantVariance> vec);
#endif       
		CPiecewiseConstantVarianceVector (std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance>>& vec, Object^ owner);
#ifdef HANDLE
		CPiecewiseConstantVarianceVector (std::vector<QuantLib::Handle<QuantLib::PiecewiseConstantVariance>>& vec, Object^ owner);
#endif
		virtual ~CPiecewiseConstantVarianceVector ();
		!CPiecewiseConstantVarianceVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IPiecewiseConstantVariance^ default[int] 
		{
			virtual IPiecewiseConstantVariance^ get (int index) = Core::INativeVector<IPiecewiseConstantVariance^>::default::get;
			virtual void set (int index, IPiecewiseConstantVariance^ value) = Core::INativeVector<IPiecewiseConstantVariance^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IPiecewiseConstantVariance^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IPiecewiseConstantVariance^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IPiecewiseConstantVariance^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IPiecewiseConstantVariance^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::PiecewiseConstantVariance>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::PiecewiseConstantVariance>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::PiecewiseConstantVariance>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>::typeid)]
	[FactoryFor(Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance::typeid)]
	[FactoryFor(Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance_Factory::typeid)]
	public ref class CPiecewiseConstantVariance_Factory sealed : public IPiecewiseConstantVariance_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of IPiecewiseConstantVariance, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IPiecewiseConstantVariance^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IPiecewiseConstantVariance, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IPiecewiseConstantVariance^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IPiecewiseConstantVariance^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IPiecewiseConstantVariance^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IPiecewiseConstantVariance^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IPiecewiseConstantVariance^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ CreateNativeVector ()
		{
			return REFNEW CPiecewiseConstantVarianceVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Models*/ } /*Marketmodels*/ } /*Models */}
